<html>
<!-- =====================================================================

  File:      usp_ShoppingCartAddItem.htm for Adventure Works Cycles Storefront Sample
  Summary:   Self-documentation for application
  Date:	     June 16, 2003

=====================================================================

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= -->
    <head>
        <title>Adventure Works Cycles Store Documentation</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body class="NormalIndent">
        <h1>"usp_ShoppingCartAddItem" Stored Procedure</h1>

        <b>Description:</b>

        <P>This stored procedure adds a product to the ShoppingCart for the given CartID.&nbsp;
        If the product does not exist in the current cart, it adds a new entry.&nbsp; If the
        product does already exist in the current CartID, it adds the quantity to the
        quantity currently in the database. </P>

        <b>Definition:</b>

<pre style="background-color:white">
<span style="color: #0000FF"><strong>CREATE</strong></span> Procedure usp_ShoppingCartAddItem
<span style="color: #4444FF">(</span>
    @CartID <span style="color: #2040a0"><strong>nvarchar</strong></span><span style="color: #4444FF">(</span>50<span style="color: #4444FF">)</span><span style="color: #4444FF">,</span>
    @ProductID <span style="color: #2040a0"><strong>int</strong></span><span style="color: #4444FF">,</span>
    @Quantity <span style="color: #2040a0"><strong>int</strong></span>
<span style="color: #4444FF">)</span>
<span style="color: #0000FF"><strong>AS</strong></span>

DECLARE @CountItems <span style="color: #2040a0"><strong>int</strong></span>

<span style="color: #0000FF"><strong>SELECT</strong></span>
    @CountItems <span style="color: #4444FF">=</span> Count<span style="color: #4444FF">(</span>ProductID<span style="color: #4444FF">)</span>
<span style="color: #0000FF"><strong>FROM</strong></span>
    Sales.ShoppingCartItem
<span style="color: #0000FF"><strong>WHERE</strong></span>
    ProductID <span style="color: #4444FF">=</span> @ProductID
  <span style="color: #0000FF"><strong>AND</strong></span>
    ShoppingCartID <span style="color: #4444FF">=</span> @CartID;

IF @CountItems <span style="color: #4444FF">&gt;</span> 0  <span style="color: #008000">-- There are items - update the current quantity </span>

    UPDATE
        Sales.ShoppingCartItem
    <span style="color: #0000FF"><strong>SET</strong></span>
        Quantity <span style="color: #4444FF">=</span> <span style="color: #4444FF">(</span>@Quantity <span style="color: #4444FF">+</span> Quantity<span style="color: #4444FF">)</span>
    <span style="color: #0000FF"><strong>WHERE</strong></span>
        ProductID <span style="color: #4444FF">=</span> @ProductID
      <span style="color: #0000FF"><strong>AND</strong></span>
        ShoppingCartID <span style="color: #4444FF">=</span> @CartID;

ELSE  <span style="color: #008000">-- New entry for this Cart.  Add a new record </span>
    <span style="color: #0000FF"><strong>BEGIN</strong></span>

    <span style="color: #0000FF"><strong>INSERT</strong></span> <span style="color: #0000FF"><strong>INTO</strong></span> Sales.ShoppingCartItem
    <span style="color: #4444FF">(</span>
        ShoppingCartID<span style="color: #4444FF">,</span>
        Quantity<span style="color: #4444FF">,</span>
        ProductID
    <span style="color: #4444FF">)</span>
    VALUES
    <span style="color: #4444FF">(</span>
        @CartID<span style="color: #4444FF">,</span>
        @Quantity<span style="color: #4444FF">,</span>
        @ProductID
    <span style="color: #4444FF">)</span>;
    <span style="color: #0000FF"><strong>END</strong></span>



</pre>

        <b>Database Tables Used: </b>

        <P><i>ShoppingCartItem:</i> &nbsp;&nbsp;The ShoppingCartItem table keeps track of the items a user has purchased.&nbsp;
        Its primary key is the ShoppingCartItemID field.&nbsp; The ShoppingCartID is a string which is used to
        identify the user who owns the basket of items.&nbsp; There is a many to one
        relationship between the ShoppingCartItem table and the Product table.&nbsp; Note that
        if not Quantity is supplied, a default of 1 is entered.

        <p>

        <img src="1x1.gif" width=25> <IMG src="../docs/images/ShoppingCart.gif" width="175" height="167"></p>

    </body>
</html>
